package com.example.admin.myapplication.util;

import android.text.TextUtils;

import com.example.admin.myapplication.db.City;
import com.example.admin.myapplication.db.County;
import com.example.admin.myapplication.db.Province;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Admin on 2017/5/27.
 */

public class Utility {
    public static boolean handleProvinceResponse(String response){
        if (!TextUtils.isEmpty(response)) try {
            JSONArray allProvinces = new JSONArray(response);
            for (int i = 0; i < allProvinces.length(); i++) {
                JSONObject ProvinceObgect = allProvinces.getJSONObject(i);
                Province province = new Province();
                province.setProvinceName(ProvinceObgect.getString("name"));
                province.setProvinceCode(ProvinceObgect.getInt("id"));
                province.save();

            }
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static boolean handleCityResponse(String response, int provinceId){
        if (!TextUtils.isEmpty(response)) try {
            JSONArray allCities = new JSONArray(response);
            for (int i = 0; i < allCities.length(); i++) {
                JSONObject CityObgect = allCities.getJSONObject(i);
                City city = new City();
                city.setCityName(CityObgect.getString("name"));
                city.setCityCode(CityObgect.getInt("id"));
                city.setProvinceId(provinceId);
                city.save();

            }
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static boolean handleCountyResponse(String response, int cityId ){
        if (!TextUtils.isEmpty(response)) try {
            JSONArray allCounties = new JSONArray(response);
            for (int i = 0; i < allCounties.length(); i++) {
                JSONObject CountyObgect = allCounties.getJSONObject(i);
                County county = new County();
                county.setCountyName(CountyObgect.getString("name"));
                county.setWeatherId(CountyObgect.getString("weather_id"));
                county.setCitId(cityId);
                county.save();

            }
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
}
